package net.lcqf.tcp.game.match;

import net.lcqf.tcp.game.match.model.MatchPlayer;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2025 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2025/3/8 21:56
 * +----------------------------------------------------------------------
 */

@Component
public class MatchFactory {

    private static final Map<TeamType, MatchBaseService> MATCH = new ConcurrentHashMap<>();

    public static void register(TeamType type, MatchBaseService matchBaseService) {
        Assert.notNull(type, "type must not be null");
        Assert.notNull(matchBaseService, "matchBaseService must not be null");
        Assert.isNull(MATCH.putIfAbsent(type, matchBaseService),"type already exists");
    }


    /**
     * 将玩家添加到队列
     * @param player 玩家信息
     * @return {@code boolean}
     */
    public boolean addPlayer(String teamType, MatchPlayer player) {
        TeamType type = TeamType.resolveEvent(teamType);

        MatchBaseService tClass = MATCH.get(type);
        return tClass.addPlayer(player);
    }


    /**
     * 将玩家移除队列
     * @param channelId 通信的唯一标识
     * @return {@code boolean}
     */
    public boolean removePlayer(String teamType, String channelId) {
        TeamType type = TeamType.resolveEvent(teamType);

        MatchBaseService tClass = MATCH.get(type);
        return tClass.removePlayer(channelId);
    }
}
